//
//  SWWechatPaymentDataFetchReq.swift
//  ChildStory
//
//  Created by linhan on 2017/1/19.
//  Copyright © 2017年 Aiya. All rights reserved.
//

import Foundation
class SWWechatPaymentDataFetchReq: SWBaseRequest
{
    //凭借支付ID获取第三方支付参数进行支付
    var payID:String = ""
    
    override init()
    {
        super.init()
        api = SWRequestAPI.AddressCreate
    }
    
    override var url:String
    {
        return "http://mpic.bbyiya.com/pay/getWxAPPPayParam"
    }
    
    override var params:Any?
    {
        return ["payId":payID]
    }
    
    override func JSONToModel(_ json:JSON, JSONString:String, JSONData:Data) -> AnyObject?
    {
        trace(json)
        //return Mapper<SWPurchaseRequisitionFetchRsp>().map(JSONString: JSONString)
        return Mapper<SWWechatPaymentDataFetchRsp>().map(JSONString: JSONString)
    }
}

class SWWechatPaymentDataFetchRsp: NSObject,Mappable
{
    var statu:String?
    var paymentData:SWWechatPaymentDataRsp?
    private var _data:String?{
        didSet{
            if let data = _data,data.length > 0{
                paymentData = Mapper<SWWechatPaymentDataRsp>().map(JSONString: data)
            }
        }
    }
    
    
    required init?(map: Map) { }
    
    // Mappable
    func mapping(map: Map) {
        statu                   <- map["Statu"]
        _data                    <- map["BaseModle"]
    }
}

class SWWechatPaymentDataRsp: NSObject,Mappable
{
    /** 商家向财付通申请的商家id */
    var partnerId:String?
    
    /** 预支付订单 */
    var prepayId:String?
    
    /** 随机串，防重发 */
    var nonceStr:String?
    
    /** 时间戳，防重发 */
    var timeStamp:String?
    
    /** 商家根据财付通文档填写的数据和签名 */
    var package:String?
    
    /** 商家根据微信开放平台文档对数据做的签名 */
    var sign:String?
    
    
    required init?(map: Map) { }
    
    // Mappable
    func mapping(map: Map) {
        partnerId                   <- map["partnerId"]
        prepayId                   <- map["prepayId"]
        nonceStr                   <- map["nonceStr"]
        timeStamp                   <- map["timeStamp"]
        package                   <- map["package"]
        sign                      <- map["sign"]
    }
}

extension Wechat.PayData
{
    init?(response:SWWechatPaymentDataRsp)
    {
        self.init()
        guard let prepayId = response.prepayId,prepayId != "" else{
            return nil
        }
        self.prepayId = prepayId
        partnerId = response.partnerId
        nonceStr = response.nonceStr
        timeStamp = (response.timeStamp ?? "0").intValue
        package = response.package
        sign = response.sign
    }
}

